﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class TaskEditNew : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           

            if (!IsPostBack)
            {
                var id = Request.QueryString["id"];

                if (!string.IsNullOrEmpty(id))
                {
                    var sql = string.Format("select * from Task where id={0}", id);

                    var dt = Dbhelper.GetDatatable(sql);

                    if (dt.Rows.Count > 0)
                    {
                        var row = dt.Rows[0];

                        this.txtid.Text = row["id"].ToString();

                        this.txtname.Text = row["TaskName"].ToString();

                        this.txtcontent.Text = row["TaskContent"].ToString();

                        this.txtRemarks.Text = row["TaskRemarks"].ToString();
                    }
                }
            }
        }

        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            var id = this.txtid.Text;
            var name = this.txtname.Text;
            var content = this.txtcontent.Text;
            var remarks = this.txtRemarks.Text;

            if (string.IsNullOrEmpty(id))
            {
                //var userId = Session["UserId"].ToString();

                var sql = string.Format("insert into Task(TaskName,TaskContent,CreateUserId,Remarks) values('{0}','{1}',{2},'{3}')",name,content,1,remarks);

                var inserted = Dbhelper.ExeOperation(sql);

                if(inserted > 0)
                {
                    var success = "<script>alert('添加成功')</script>";

                    Response.Write(success);

                    Response.Redirect("Default.aspx");
                }
                else
                {
                    var error = "<script>alert('添加失败')</script>";

                    Response.Write(error);
                }
            }
            else
            {
                var sql = string.Format("update Task set TaskName='{0}',TaskContent='{1}',Remarks='{2}',where id = {3}",name,content,remarks,id);

                var updated = Dbhelper.ExeOperation(sql);

                if(updated > 0)
                {
                    var success1 = "<script>alert('修改成功')</script>";

                    Response.Write(success1);

                    Response.Redirect("Default.aspx");
                }
                else
                {
                    var error1 = "<script>alert('修改失败')</script>";

                    Response.Write(error1);
                }
            }
            Response.Redirect("Default.aspx");
        }

        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx");
        }
    }
}